<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of PriceCreateAction
 *
 * @author Levz
 */
class PriceCreateAction extends CAction {

    public function run() {
        $model = new Price;
        
        // Uncomment the following line if AJAX validation is needed
        $this->performAjaxValidation($model);

        if (isset($_POST['Price'])) {
            $model->attributes = $_POST['Price'];
            if ($model->save()) {
                if (Yii::app()->request->isAjaxRequest) {
                    Yii::app()->user->setFlash('success', 'Price has been successfully added to the database.');

                    echo CJSON::encode(array(
                        'status' => 'success',
                        'div' => $this->controller->renderPartial('/app/forms/price_entry_modal', array('model' => $model), true, true)
                    ));
                    exit;
                }
                else
                    $this->controller->redirect(Yii::app()->user->returnUrl);
            }
        }

        if (Yii::app()->request->isAjaxRequest) {
            echo CJSON::encode(array(
                'status' => 'failure',
                'div' => $this->controller->renderPartial('/app/forms/price_entry_modal', array('model' => $model), true, true)
            ));
            exit;
        }
        else
            $this->controller->render('/app/forms/price_entry', array(
                'model' => $model,
            ));
    }

    /**
     * Performs the AJAX validation.
     * @param CModel the model to be validated
     */
    protected function performAjaxValidation($model) {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'price-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }

}

?>
